//
//  LoginViewController.m
//  移动营销
//
//  Created by wmm on 16/2/24.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "LoginViewController.h"
#import "AFNetworking.h"
#import "JudgeNumber.h"
#import "MyTabBarController.h"
#import "ForgetPassViewController.h"
#import "UserModel.h"

@interface LoginViewController (){
    
}

@property (nonatomic, strong) UITextField *phoneFld;
@property (nonatomic, strong) UITextField *passFld;

@property (nonatomic, strong) UIButton *remPassBtn;
@property (readonly, nonatomic) int isRemPass;//1记住2不记住

@property (nonatomic, strong) UIButton *loginBtn;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setBackgroundImage:[ViewTool getImageFromColor:NavBackColor WithRect:CGRectMake(0, 0, KSCreenW, 64.0f)] forBarMetrics:UIBarMetricsDefault];
    
     self.navigationController.navigationBar.translucent = NO;
    CGFloat statusBarHeight = 0;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
    {
        statusBarHeight = 20;
    }
    self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:@"登录"] ;
    self.navigationItem.leftBarButtonItem=[ViewTool getBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
    
//    UILabel *phoneLab = [[UILabel alloc] initWithFrame:CGRectMake([UIView getWidth:30.0f], 64 +  [UIView getWidth:30.0f], 80, 22)];
//    phoneLab.text = @"手机号";
//    phoneLab.textColor = blackFontColor;
//    phoneLab.font = [UIFont systemFontOfSize:16];
//    [self.view addSubview:phoneLab];
    
    
    
    UIImageView *phoneImg = [[UIImageView alloc] initWithFrame:CGRectMake([UIView getWidth:15.0f] , [UIView getWidth:30.0f] , 45, 45)];
    phoneImg.image = [UIImage imageNamed:@"HY_login_icon_phone1"];
    [self.view addSubview:phoneImg];
    
//    UILabel *phoneLab = [[UILabel alloc] initWithFrame:CGRectMake(50,phoneImg.y, 80, 22)];
//    phoneLab.text = @"手机号";
//    phoneLab.textColor = blackFontColor;
//    phoneLab.font = [UIFont systemFontOfSize:16];
//    [self.view addSubview:phoneLab];
    _phoneFld = [[UITextField alloc] initWithFrame:CGRectMake(phoneImg.maxX + 10, phoneImg.y+ 13, KSCreenW - [UIView getWidth:15.0f] - phoneImg.maxX - 20, 22)];
    _phoneFld.placeholder = @"请输入手机号";
    _phoneFld.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneFld.keyboardType = UIKeyboardTypePhonePad;//电话键盘
    [_phoneFld setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    _phoneFld.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:_phoneFld];
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(_phoneFld.x,phoneImg.maxY , KSCreenW-_phoneFld.x - [UIView getWidth:30.0f], 1)];
    line.layer.borderWidth = 18;
    line.layer.borderColor = [grayLineColor CGColor];
    [self.view addSubview:line];
    
//    UILabel *checkLab = [[UILabel alloc] initWithFrame:CGRectMake(phoneLab.x,line.maxY + [UIView getWidth:30.0f], 80, 22)];
//    checkLab.text = @"登录密码";
//    checkLab.textColor = blackFontColor;
//    checkLab.font = [UIFont systemFontOfSize:16];
//    [self.view addSubview:checkLab];
    
    UIImageView *checkImg = [[UIImageView alloc] initWithFrame:CGRectMake(phoneImg.x,line.maxY + [UIView getWidth:25.0f], 45, 45)];
    checkImg.image = [UIImage imageNamed:@"HY_login_icon_password1.png"];
    [self.view addSubview:checkImg];
//    UILabel *checkLab = [[UILabel alloc] initWithFrame:CGRectMake(50,phoneImg.y+57, 80, 22)];
//    checkLab.text = @"登录密码";
//    checkLab.textColor = blackFontColor;
//    checkLab.font = [UIFont systemFontOfSize:16];
//    [self.view addSubview:checkLab];
    _passFld = [[UITextField alloc] initWithFrame:CGRectMake(checkImg.maxX + 10,checkImg.y+ 13, _phoneFld.width, 22)];
    _passFld.placeholder = @"请输入6~20位密码";
    _passFld.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passFld.returnKeyType =UIReturnKeyGo;//return键
    _passFld.secureTextEntry = YES;//密码
    [_passFld setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    _passFld.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:_passFld];
    UILabel *line2 = [[UILabel alloc] initWithFrame:CGRectMake(line.x,checkImg.maxY , line.width, 1)];
    line2.layer.borderWidth = 1;
    line2.layer.borderColor = [grayLineColor CGColor];
    [self.view addSubview:line2];
    
    //是东方闪电发送的 我 23
    _remPassBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [_remPassBtn setBackgroundImage:[UIImage imageNamed:@"HY_list_btn_checkbox_selected"] forState:UIControlStateNormal];
    _remPassBtn.frame = CGRectMake(phoneImg.centerX - 35 / 2.0f, line2.maxY + 8.0f, 35, 35);
    [_remPassBtn addTarget:self action:@selector(changeRemPass) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_remPassBtn];
    
    UILabel *remPassLab = [[UILabel alloc] initWithFrame:CGRectMake(_remPassBtn.maxX + 5,_remPassBtn.y + _remPassBtn.height / 2.0f - 22 / 2.0f, 80, 22)];
    remPassLab.text = @"记住密码";
    remPassLab.textColor = REmemberpassColor;
    remPassLab.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:remPassLab];
    
    UIButton *forgetPassBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [forgetPassBtn setTitle:NSLocalizedString(@"忘记密码？", nil) forState:UIControlStateNormal];
    [forgetPassBtn setTitleColor:greenBackColor forState:UIControlStateNormal];
    [forgetPassBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    
    forgetPassBtn.frame = CGRectMake(KSCreenW-100,remPassLab.y, 80, 22);
    [forgetPassBtn addTarget:self action:@selector(forgetPass) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetPassBtn];
    
    _loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [_loginBtn setTitle:NSLocalizedString(@"确认登录", nil) forState:UIControlStateNormal];
    [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_loginBtn.titleLabel setFont:[UIFont systemFontOfSize:20]];
//    [_loginBtn setBackgroundColor:redBackColor];
    [_loginBtn setBackgroundImage:[UIImage imageNamed:@"HY_btn_red_normal.png"] forState:UIControlStateNormal];
    [_loginBtn setBackgroundImage:[UIImage imageNamed:@"HY_btn_red_press.png"] forState:UIControlStateHighlighted];
//     [_loginBtn setBackgroundImage:[UIImage imageNamed:@"HY_btn_red_press.png"] forState:UIControlStateHighlighted];
    _loginBtn.layer.cornerRadius = 4.0;//圆角
    _loginBtn.frame = CGRectMake(20, KSCreenH  - 64 - 64, KSCreenW-40, 60);
    _loginBtn.userInteractionEnabled=NO;//不可点击
    _loginBtn.alpha=0.4;//变灰
    [_loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_loginBtn];
    
    _phoneFld.delegate = self;
    _passFld.delegate = self;
    
    [_phoneFld addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    [_passFld addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    _isRemPass = [[userDefaults objectForKey:@"isRemPass"] intValue];
    if(_isRemPass == 1){
        _phoneFld.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
        _passFld.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
        [_remPassBtn setBackgroundImage:[UIImage imageNamed:@"HY_list_btn_checkbox_selected"] forState:UIControlStateNormal];
        
        _loginBtn.userInteractionEnabled=YES;//不可点击
        _loginBtn.alpha=1;//变灰
        
    }else if(_isRemPass == 2){
        [_remPassBtn setBackgroundImage:[UIImage imageNamed:@"HY_list_btn_checkbox_normal"] forState:UIControlStateNormal];
    }else{
        
        _isRemPass = 1;
        [_remPassBtn setBackgroundImage:[UIImage imageNamed:@"HY_list_btn_checkbox_selected"] forState:UIControlStateNormal];
        [userDefaults setInteger:_isRemPass forKey:@"isRemPass"];
        [userDefaults synchronize];
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}
//- (void)viewDidAppear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)goToBack{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)changeRemPass{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if(_isRemPass == 1){
        [_remPassBtn setBackgroundImage:[UIImage imageNamed:@"HY_list_btn_checkbox_normal"] forState:UIControlStateNormal];
        _isRemPass = 2;
        [userDefaults setInteger:_isRemPass forKey:@"isRemPass"];
        
    }else if(_isRemPass == 2){
        [_remPassBtn setBackgroundImage:[UIImage imageNamed:@"HY_list_btn_checkbox_selected"] forState:UIControlStateNormal];
        _isRemPass = 1;
        [userDefaults setInteger:_isRemPass forKey:@"isRemPass"];
    }
    [userDefaults synchronize];
}

- (void)forgetPass{
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
    [[UINavigationBar appearance] setTintColor:mainOrangeColor];
    [self.navigationItem setBackBarButtonItem:backItem];
    ForgetPassViewController *forgetPassViewController = [[ForgetPassViewController alloc] init];
    [self.navigationController pushViewController:forgetPassViewController animated:YES];
}

- (void)login{
    if (_phoneFld.text == nil | [_phoneFld.text length] == 0 ){
        [self.view makeToast:@"请输入手机号"];
        return;
    }else if (![JudgeNumber boolenPhone:_phoneFld.text]){
        [self.view makeToast:@"请输入正确的手机号"];
        return;
    }else if (_passFld.text == nil | [_passFld.text length] == 0 ){
        [self.view makeToast:@"请输入密码"];
        return;
    }else if ([_passFld.text length] < 6 ){
        [self.view makeToast:@"密码不小于6位"];
        return;
    }
    [_phoneFld resignFirstResponder];
    [_passFld resignFirstResponder];
    
    //    [DataTool login:_phoneFld.text withPass:_passFld.text fromView:self];
//       _myTabBarController = [[MyTabBarController alloc] init];
    //    [self presentViewController:_myTabBarController animated:YES completion:nil];
    //
    //
//        MyNavigationController *myNavigationController = self.navigationController;
    
//        [self.navigationController presentViewController:_myTabBarController animated:YES completion:nil];
    //
//        return;
    
    [self toLogin];
}

- (void)toLogin{
#warning 注意zhushi
//    _myTabBarController = [[MyTabBarController alloc] init];
//    [self presentViewController:_myTabBarController animated:YES completion:nil];

//    [self.view makeToastActivity];
    [SVProgressHUD show];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary * params = [NSDictionary dictionaryWithObjectsAndKeys:_phoneFld.text, @"username", _passFld.text, @"password", nil];

    [DataTool postWithUrl:LOGIN_URL parameters:params success:^(id data) {
    id backdata = CRMJsonParserWithData(data);
          NSLog(@"登陆数据%@",backdata);
    int code = [[backdata objectForKey:@"code"] intValue];
        if([backdata[@"errorCode"] intValue] == 4){
            [self.view makeToast:@"对不起，登陆账号已不能使用"];
            return;
        }else if([backdata[@"errorCode"] intValue] == 2){
             [self.view makeToast:@"对不起，登陆账号已过期"];
        }else if(code != 100){
        
        NSString * message = [(NSDictionary *)backdata objectForKey:@"message"];
        [self.view makeToast:message];
        [self.view hideToastActivity];
        [SVProgressHUD dismiss];
            
        return ;
    }else{
       
        NSString * token = [(NSDictionary *)backdata objectForKey:@"token"];
        NSDictionary *user = [(NSDictionary *)backdata objectForKey:@"user"];
        
        NSDictionary *dict = [DataTool changeType:user];
//        NSLog(@"登陆数据%@",dict);
        
        UserModel *loginer = [[UserModel alloc] init];
        [loginer setValuesForKeysWithDictionary:dict];
        // [userDefaults setObject:loginer forKey:@"user"];
        
        NSString * userid = (NSString *)[dict objectForKey:@"uid"];
        NSString * username = (NSString *)[dict objectForKey:@"username"];
        NSString * department = (NSString *)[dict objectForKey:@"department"];
        NSString * departmentName = (NSString *)[dict objectForKey:@"departmentName"];
        NSString * img = (NSString *)[dict objectForKey:@"img"];
        NSString * logo = (NSString *)[dict objectForKey:@"logo"];
        NSString * name = (NSString *)[dict objectForKey:@"name"];
        NSString * type = (NSString *)[dict objectForKey:@"type"];
//        NSString *mName = (NSString *)[dict objectForKey:@"mname"];
        //          NSString *type = [[[NSUserDefaults standardUserDefaults] objectForKey:@"type"] stringValue];//取值
        
        NSLog(@"%@",type);
        
        [userDefaults setObject:token forKey:@"token"];
        [userDefaults setObject:userid forKey:@"uid"];
        [userDefaults setObject:username forKey:@"username"];
        [userDefaults setObject:department forKey:@"department"];
        [userDefaults setObject:type forKey:@"type"];
        [userDefaults setObject:img forKey:@"img"];
        [userDefaults setObject:logo forKey:@"logo"];
        [userDefaults setObject:name forKey:@"name"];
        [userDefaults setObject:departmentName forKey:@"departmentName"];
//        [userDefaults setObject:mName forKey:@"managerName"];
        [userDefaults setObject:_passFld.text forKey:@"password"];
        
        [userDefaults synchronize];
        
        NSDictionary *dic = @{@"token" :TOKEN,@"uid" : @(UID)};
        NSLog(@"参数%@",dic);
        [DataTool postWithUrl:GET_MANAGER_INFO parameters:dic success:^(id data) {
            id backD = CRMJsonParserWithData(data);
            NSLog(@"管理员数据%@",backD);
            NSDictionary *managerDict = [DataTool changeType:backD];
            [userDefaults setObject:managerDict forKey:@"managerInfo"];
            [userDefaults synchronize];
            
        } fail:^(NSError *error) {
            NSLog(@"%@",error.localizedDescription);
        }];
        
        
        _myTabBarController = [[MyTabBarController alloc] init];
//        _myTabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [SVProgressHUD dismiss];
        [self presentViewController:_myTabBarController animated:NO completion:nil];
    }
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
//        [self.view hideToastActivity];
        [SVProgressHUD dismiss];
        [self.view makeToast:@"登录失败"];
    }];
}

- (void)textFieldChanged:(UITextField *)textField{
    if (textField == _phoneFld) {
        if ([_phoneFld.text length] > 20) {
            _phoneFld.text = [_phoneFld.text substringToIndex:20];
        }
    }
    if (textField == _passFld) {
        if ([_passFld.text length] > 20) {
            _passFld.text = [_passFld.text substringToIndex:20];
        }
    }
    if(_phoneFld.text != nil & [_phoneFld.text length] != 0 & _passFld.text != nil & [_passFld.text length] != 0 ){
        _loginBtn.userInteractionEnabled=YES;//可点击
        _loginBtn.alpha=1;
    }else{
        _loginBtn.userInteractionEnabled=NO;
        _loginBtn.alpha=0.4;
    }
}

//关闭虚拟键盘。
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if(textField == _passFld){
        [_phoneFld resignFirstResponder];
        [_passFld resignFirstResponder];
        [self login];
    }
    return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_phoneFld resignFirstResponder];
    [_passFld resignFirstResponder];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
